package com.imooc.aspectJ.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 切面类
 *
 * @author wangc
 */

@Aspect
public class MyAspectAnno {
    /**
     * 所有方法前置增强
     *
     * @Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.*(..))")
     */

    /**
     * 在方法之前前置增强
     *
     * @Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))")
     */
    @Before(value = "myPointcut1()")
    public void before(JoinPoint joinPoint) {
        System.out.println("前置通知========================" + joinPoint);
    }

    /**
     * 在方法之后后置增强
     * 通过returning属性可以定义方法返回值，作为参数
     */
    @AfterReturning(value = "myPointcut2()", returning = "result")
    public void afterReturing(Object result) {
        System.out.println("后置通知=============================" + result);
    }

    /**
     * 环绕通知增强
     */
    @Around(value = "myPointcut3()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前通知====================");

        /**
         * 执行目标方法
         */
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("环绕后通知====================");
        return obj;
    }

    /**
     * 异常抛出通知增强
     */
    @AfterThrowing(value = "myPointcut4()", throwing = "e")
    public void afterThrowing(Throwable e) {
        System.out.println("异常抛出通知===================" + e);
    }

    @After(value = "myPointcut5()")
    public void aftet() {
        System.out.println("最终通知======================");
    }

    /**
     * 通过@Pointcut为切点命名
     */
    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))")
    public void myPointcut1() {
    }

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))")
    public void myPointcut2() {
    }

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))")
    public void myPointcut3() {
    }

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.fianOne(..))")
    public void myPointcut4() {
    }

    @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.fianAll(..))")
    public void myPointcut5() {
    }
}
